(function() {
	var JSONRequest = tinymce.util.JSONRequest, each = tinymce.each, DOM = tinymce.DOM;

	tinymce.create('tinymce.plugins.mLanguagePlugin', {
		getInfo : function() {
			return {
				longname : 'mLanguage',
				author : 'Sirlon',
				authorurl : 'http://www.gnomx.at',
				infourl : 'http://www.gnomx.at',
				version : tinymce.majorVersion + "." + tinymce.minorVersion
			};
		},

		init : function(ed, url) {
			var t = this, cm;

			t.url = url;
			t.editor = ed;
			
			
			
			
		},

		createControl: function(n, cm) {
			var t = this, c, ed = t.editor;
			var lang = 'en';
        switch (n) {
            case 'mlang':
				var c = cm.createSplitButton('mlang', {
				title : 'mLanguage',
				image : t.url+'/mlang.gif',
				onclick : function() {
                        ed.selection.setContent('['+lang+']'+ed.selection.getContent({format : 'text'})+'[/'+lang+']');
					
                    }
			});
			
			c.onRenderMenu.add(function(c, m) {
                    m.add({title : 'Tag it.', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
					
					JSONRequest.sendRPC({
    					url : t.url+'/langs.php',
						method : 'getSuggestions',
						params : null,
    					success : function(r) {
						
							each(r, function(v) {
				
        					m.add({title : v, onclick : function(){
														lang = v;
														ed.selection.setContent('['+v+']'+ed.selection.getContent({format : 'text'})+'[/'+v+']');
														this.setSelected(1);
																 } });
											 });
    					},
						error : function(e, x) {
						alert(e.errstr || ('Error response: ' + x.responseText));
						}
					});
                });
				return c;
			}
				return null;
		}, 

	});

	// Register plugin
	tinymce.PluginManager.add('mlang', tinymce.plugins.mLanguagePlugin);
})();